package com.lanou.student.page;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import com.lanou.student.bean.User;
import com.lanou.student.controller.UserController;
import com.lanou.student.util.PasswordUtil;

/**
 * 注册页面
 * @author Lenovo
 *
 */
public class Regester {
	private Scanner sc = new Scanner(System.in);
	//正则表达式
	private final String REGEX_USERNAME = "^[a-zA-Z0-9]{6,11}$";
	private final String REGEX_PASSWORD = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,11}$";
	private final String REGEX_EMAIL = "^([a-z0-9A-Z]+[_-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	private final String REGEX_PHONE = "^((13[0-9])|(15[^4,\\D])|(16[0-9])|(18[0-9])|(17[0-9]))\\d{8}$";
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private UserController userController;
	
	
	public Regester() {
		userController = new UserController();
	}
	
	
	
	
	
	public boolean regester() {
		User user = getUser();
		return userController.regester(user);
		
	}
	
	public User getUser() {
		System.out.println("请输入用户名");
		String userName = sc.next();
		//校验注册的用户名格式是否正确
//		while (!(userName=sc.next()).matches(REGEX_USERNAME)) {
//			//校验失败
//			System.out.println("请输入用户名不合法，请重新输入");
//		}
		
		//校验用户名是否重复
		while (userController.getUser(userName,null) != null) {
			System.out.println("请输入用户名已经被注册，请重新输入");
			userName=sc.next();
			
		}
		
		System.out.println("请输入密码");
		String password = sc.next();
//		while (!(password=sc.next()).matches(REGEX_PASSWORD)) {
//			//校验失败
//			System.out.println("请输入密码不合法，请重新输入");
//		}
		
		
		String newPass = PasswordUtil.md5(password,userName);
		System.out.println( "请输入邮箱地址");
		String email = sc.next();
//		while (!(email=sc.next()).matches(REGEX_EMAIL)) {
//			//校验失败
//			System.out.println("请输入邮箱不合法，请重新输入");
//		}
		System.out.println("请输入联系方式");
		String phone = sc.next();
//		while (!(phone=sc.next()).matches(REGEX_PHONE)) {
//			//校验失败
//			System.out.println("请输入手机号不合法，请重新输入");
//		}
//		System.out.println("是否是管理员");
//		boolean isRoot = sc.nextBoolean();
		
		String createTime = sdf.format(new Date());
		return new User(userName,newPass,email,phone,createTime);
	}

}
